#ifndef QUEUE_H
#define QUEUE_H

#include "common.h"

#define QUEUE_SIZE 256

/**
 * FixedQueue is a queue that uses an fixed array to store its elements
 */

template< class T >
class FixedQueue
{
    T mData[QUEUE_SIZE];
    int head;
    int tail;

    void toNextAvlPos()
    {
            tail = (tail+1)%QUEUE_SIZE;
    }

public:
    FixedQueue():head(0),tail(0){}

    void enter( T element )
    {
        if( head == tail )
        {
            head = tail = 0; //Empty queue
        }

        cout<<"Tail1="<<tail<<endl;
        tail = (tail+1)%QUEUE_SIZE;
        cout<<"Tail2="<<tail<<endl;
        if( head != tail )
        {
            mData[tail] = element;
        }
        else
        {
            cerr<<"Head:"<<head<<" Tail:"<<tail<<endl;
            Exception e("Queue overflow");
            throw e;
        }
    }

    T leave()
    {
        if( empty() )
        {
            cerr<<"Head:"<<head<<" Tail:"<<tail<<endl;
            Exception e("Queue underflow");
            throw e;
        }

        head = (head+1)%QUEUE_SIZE;
        if( head == tail )
        {
            head=tail=0;
        }
        return mData[head];
    }

    bool empty() const { return head == tail; }
};

#endif // QUEUE_H
